<?php
/**
 * Created by IntelliJ IDEA.
 * User: jimmyhsu
 * Date: 2017/4/24
 * Time: 18:47
 */
date_default_timezone_set("Asia/Shanghai");
include ('checkLogin.php');
$course_id = $_POST['course_id'];
$title = $_POST['title'];
$author_id = $_POST['author'];
$content = $_POST['content'];
if (mb_strlen($title,'UTF8') > 20 || mb_strlen($content,'UTF8') > 100) {
    die('text length exceed');
}
$b_id = 0;
$query = mysqli_query($cn, "select max(b_id) as latest_id from bulletin where course_id = '$course_id'");
if ($query) {
    $b_id = mysqli_fetch_array($query)['latest_id'];
}
if ($b_id == '') {
    $b_id = 0;
} else {
    $b_id++;
}
$sql = "insert into bulletin values('$course_id', $b_id, '$title', '$content', $author_id, '".date("Y-m-d H:i:s")."')";
//echo $sql;
$query = mysqli_query($cn, $sql);
if ($query) {
    echo "success";
} else {
    echo "db insert failed";
}